# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# $Id: feeds.py 15 2008-04-01 00:27:31Z semente $
# ----------------------------------------------------------------------------
#
#  Copyright (c) 2008 Guilherme Mesquita Gondim
#
#  This file is part of django-fleshin.
#
#  django-fleshin is free software under terms of the GNU General
#  Public License version 3 (GPLv3) as published by the Free Software
#  Foundation. See the file README for copying conditions.
#

from django.conf import settings
from django.contrib.comments.models import FreeComment
from django.contrib.syndication.feeds import Feed
from django.contrib.syndication.feeds import FeedDoesNotExist
from django.utils.feedgenerator import Atom1Feed

from django.contrib.sites.models import Site
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _

from fleshin.models import Photo, Album
from fleshin.settings import HAS_TAG_SUPPORT

if HAS_TAG_SUPPORT:
    from tagging.models import Tag

class RssPhotosFeed(Feed):
    """
    All photos RSS feed.
    """
    description = _('Latest photos on site')
    title_template = 'feeds/fleshin_photo_title.html'
    description_template = 'feeds/fleshin_photo_description.html'

    def title(self):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _("%(title)s's photos") % {'title': self._site.name}

    def link(self):
        return reverse('fleshin-photo-list')

    def get_query_set(self):
        return Photo.published_on_site.order_by('-pub_date')

    def items(self):
        return self.get_query_set()[:15]

    def item_pubdate(self, item):
        return item.pub_date

    def item_categories(self, item):
        try:
            return item.tags.split()
        except AttributeError:
            pass      # ignore if not have django-tagging support

class AtomPhotosFeed(RssPhotosFeed):
    """
    All photos Atom feed.
    """
    feed_type = Atom1Feed
    subtitle = RssPhotosFeed.description



class RssAlbumsFeed(Feed):
    """
    All photo albums RSS feed.
    """
    description = _('Latest photo albums on site')
    title_template = 'feeds/fleshin_album_title.html'
    description_template = 'feeds/fleshin_album_description.html'

    def title(self):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _("%(title)s's photo albums") % {'title': self._site.name}

    def link(self):
        return reverse('fleshin-photo-list')

    def get_query_set(self):
        return Album.objects.all()

    def items(self):
        return self.get_query_set()[:15]

class AtomAlbumsFeed(RssAlbumsFeed):
    """
    All photo albums Atom feed.
    """
    feed_type = Atom1Feed
    subtitle = RssAlbumsFeed.description



class RssPhotosByTagFeed(RssPhotosFeed):
    """
    RSS feeds for photos divided by tags.
    """
    def get_object(self, bits):
        # In case of "rss/tag/example/", or other such clutter,
        # check that bits has only one member.
        if len(bits) != 1:
            raise ObjectDoesNotExist
        return Tag.objects.get(name__exact=bits[0])

    def title(self, obj):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _('%(title)s: photos tagged "%(tag name)s"') % \
               {'title': self._site.name, 'tag name': obj.name}

    def description(self, obj):
        return _('Latest photos for tag "%(tag name)s"') % \
               {'tag name': obj.name}

    def link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        return reverse('fleshin-tagged-photo-list', args=[obj.name])

    def get_query_set(self):
        queryset = Photo.published_on_site.filter(tags__contains=obj.name)
        return queryset.order_by('-pub_date')

    def items(self, obj):
        return self.get_query_set()[:15]

class AtomPhotosByTagFeed(RssPhotosByTagFeed):
    """
    Atom feeds for photos divided by tags.
    """
    feed_type = Atom1Feed
    subtitle = RssPhotosByTagFeed.description



class RssPhotosByAlbumFeed(RssPhotosFeed):
    """
    RSS feeds for photos divided by albums.
    """
    def get_object(self, bits):
        # In case of "rss/album/example/", or other such clutter,
        # check that bits has only one member.
        if len(bits) != 1:
            raise ObjectDoesNotExist
        return Album.objects.get(name__exact=bits[0])

    def title(self, obj):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _('%(title)s: photos of album "%(album name)s"') % \
               {'title': self._site.name, 'album name': obj.name}

    def description(self, obj):
        return _('Latest photos for album "%(album name)s"') % \
               {'album name': obj.name}

    def link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        return reverse('fleshin-photo-album-list', args=[obj.name])

    def get_query_set(self):
        queryset = Photo.published_on_site.filter(album__exact=obj.name)
        return queryset.order_by('-pub_date')

    def items(self, obj):
        return self.get_query_set()[:15]

class AtomPhotosByAlbumFeed(RssPhotosByAlbumFeed):
    """
    Atom feeds for photos divided by albums.
    """
    feed_type = Atom1Feed
    subtitle = RssPhotosByAlbumFeed.description



class RssFreeCommentsFeed(Feed):
    description = _('Latest comments on photo albums')
    title_template = 'feeds/comments_title.html'
    description_template = 'feeds/comments_description.html'

    def title(self):
        if not hasattr(self, '_site'):
            self._site = Site.objects.get_current()
        return _("%(title)s's photo comments") % \
               {'title': self._site.name}

    def link(self):
        return reverse('fleshin-album-list')

    def item_pubdate(self, item):
        return item.submit_date

    def get_query_set(self):
        get_list_function = FreeComment.objects.filter
        kwargs = {
            'is_public': True,
            'site__pk': settings.SITE_ID,
            'content_type__app_label__exact': 'fleshin',
            'content_type__model__exact': 'photo',
        }
        return get_list_function(**kwargs)

    def items(self):
        return self.get_query_set()[:30]

class AtomFreeCommentsFeed(RssFreeCommentsFeed):
    feed_type = Atom1Feed
    subtitle = RssFreeCommentsFeed.description
